package com.ido.otaclient.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDialog;
import androidx.fragment.app.FragmentActivity;

import com.ido.otaclient.R;
import com.ido.otaclient.base.BaseDialogFragment;
import com.ido.otaclient.util.DipPixelUtil;
import com.ido.otaclient.util.WindowUtil;


/**
 * Waiting Dialog
 * Created by lize on 2017/5/8.
 */

public class WaitingDialog extends BaseDialogFragment {

    private static final String INTENT_EXTRA_MESSAGE = "intent_extra_message";
    private static WaitingDialog sWaitingDialog = null;

    public static WaitingDialog newInstance(String message) {
        WaitingDialog waitingDialog = new WaitingDialog();
        Bundle arguments = new Bundle();
        arguments.putString(INTENT_EXTRA_MESSAGE, message);
        waitingDialog.setArguments(arguments);
        return waitingDialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                    WindowUtil.getWindowSize().getHeight() - DipPixelUtil.dip2px(100));
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String message = getArguments().getString(INTENT_EXTRA_MESSAGE,"");

        AppCompatDialog progressDialog=new AppCompatDialog(getActivity(), R.style.TransparentStyle);
        progressDialog.setContentView(R.layout.dialog_waiting);
        TextView waitingMessageTv = (TextView) progressDialog.findViewById(R.id.waiting_tv);
        if (waitingMessageTv!=null)
            waitingMessageTv.setText(message);

        progressDialog.setCanceledOnTouchOutside(false);//点击外部不会消失
        backToActivity(progressDialog);
        return progressDialog;
    }

    /**
     * 监听返回键并回调activity的back键方法
     * @param progressDialog
     */
    private void backToActivity(AppCompatDialog progressDialog) {
        progressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (mIsBackToActivity && getActivity() != null) {
                        getActivity().onBackPressed();
                    }
                    return false;
                } else {
                    return true;
                }
            }
        });
    }

    private boolean mIsBackToActivity =false;//按返回键是否回调activity的back键方法

    /**
     * 设置是否监听按键事件并在按下返回键时回调activity的back键方法
     * @param isBack true or false
     */
    public void setBackToActivity(boolean isBack){
        this.mIsBackToActivity = isBack;
    }

    /**
     * 对话框弹出后按返回键不消失;
     * @param activity
     */
    public static void showDialogUncancel(FragmentActivity activity) {
        showDialog(activity, false,null);
    }

    public static void showDialogUncancel(FragmentActivity activity, String message) {
        showDialog(activity, false, message);
    }

    /**
     * 对话框弹出后按返回键会消失;
     * @param activity
     */
    public static void showDialog(FragmentActivity activity) {
        showDialog(activity, true, null);
    }

    public static void showDialog(FragmentActivity activity, String message) {
        showDialog(activity, true, message);
    }

    /**
     * 对话框弹出后按返回键消失且会执行其获取的activity的onBackPressed()方法
     * @param activity
     */
    public static void showDialogBack(FragmentActivity activity) {
        showDialog(activity, true, null);
        sWaitingDialog.setBackToActivity(true);
    }

    public static void showDialogBack(FragmentActivity activity, String message) {
        showDialog(activity, true, message);
        sWaitingDialog.setBackToActivity(true);
    }

    private static void showDialog(FragmentActivity activity, boolean cancelAble, String message) {
        if (!isShowing()) {
            sWaitingDialog = newInstance(message);
            sWaitingDialog.setCancelable(cancelAble);
            sWaitingDialog.show(activity.getSupportFragmentManager());
        }
    }

    public static void hideDialog() {
        if(sWaitingDialog != null && sWaitingDialog.getFragmentManager() != null) {
            sWaitingDialog.dismissAllowingStateLoss();
        }
    }

    public static void hideDialogDelay(long delay) {
        new Handler().postDelayed(WaitingDialog::hideDialog, delay);
    }

    public static boolean isShowing() {
        return sWaitingDialog != null
                && sWaitingDialog.getDialog() != null
                && sWaitingDialog.getDialog().isShowing();
    }

}
